<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/31
 * Time: 17:50
 */

namespace Admin\Controller;


use Think\Controller;

class AccessController extends Controller
{
    public function index(){
        $node=M('node');
        $res=$node->select();
        $access= M('access')->where('role_id='.I('id'))->getField('node_id',true);
        //$m=M('access')->getLastSql();
        //dump($access);exit();
        foreach ($res as $k => $v){
            if(in_array($v['id'],$access)){
                $res[$k]['select'] = 1;
            }else{
                $res[$k]['select'] = 0;
            }
        }
        $res=getSon($res,0);
        //dump($res);exit();
        $this->assign('res',$res);
        $this->assign('role_id',I('id'));
        $this->display();
    }
    public function add(){
        $addinfo=I('post.');
        $role_id=$addinfo['rid'];
        //dump($addinfo['access']);exit();
        foreach ($addinfo['access'] as $k=>$v){
            $arr=explode("_",$v);
            $addaccess[$k]['role_id']=$role_id;
            $addaccess[$k]['node_id']=$arr[0];
            $addaccess[$k]['level']=$arr[1];
        }
        if(M('access')->where('role_id='.$role_id)->delete()){
            $res=M('access')->addAll($addaccess);
            if ($res){
                echo "<script> alert('权限分配成功');</script>";
                $this->redirect('Role/index','',1,'跳转到角色列表。。。');
            }
        }


    }
}